package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jiyun.common.R.R;
import com.jiyun.entity.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.jiyun.service.UserService;

import java.util.List;


/**
 * (User)表控制层
 *
 * @author LYT
 * @since 2025-02-12 15:12:01
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("login")
    public R login(@RequestParam("email") String email,@RequestParam("password") String password) {
        User user= userService.login(email, password);
        if (user==null){
            return R.error().message("账号不存在或密码错误");
        }
        return R.ok().data("user",user).message("登陆成功");
    }

    @PostMapping("zhuce")
    public R zhuce(@RequestBody User user){
        user.setBalance(0.00);
        userService.save(user);
        return R.ok().message("注册成功,请重新登录");
    }
//完善登录信息
    @PutMapping("update")
    @ApiOperation(value = "完善用户信息")
    public R update(@RequestBody User user){
        userService.updateById(user);
        return R.ok().message("修改成功");
    }

    @GetMapping("/findAllUser")
    @ApiOperation(value = "查询所有用户")
    public R findAllUser(){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getIsdelete,0);
        List<User> userList = userService.list(wrapper);
        return R.ok().data("user",userList);
    }


    @GetMapping("deleteById")
    @ApiOperation("注销账号")
    public R deleteById(@RequestParam("userid") Integer userid){
        User user = userService.getById(userid);
        user.setIsdelete(1);
        userService.updateById(user);
        return R.ok().message("注销成功");
    }








}

